(function($) {
    "use strict";

    $.fn.tree = function() {

        return this.each(function() {
            var btn = $(this).children("a").first();
            var menu = $(this).children(".treeview-menu").first();

            var isActive = $(this).hasClass('active');

            //initialize already active menus
            if (isActive) {
                menu.show();
                btn.children(".fa-angle-down").first().removeClass("fa-angle-down").addClass("fa-angle-up");
            }
            //Slide open or close the menu on link click
            btn.click(function(e) {
                if(btn.next("ul").length==0){
                    return true;
                }else{
                    e.preventDefault();
                }


                btn.parent("li").parent().children("li").removeClass("active");
                var isreturn=btn.parent("li").parent().parent().parent().hasClass("collapsed-min");
                if(!isreturn){
                    if (isActive) {
                        //Slide up to close menu
                        menu.slideUp();
                        isActive = false;
                        btn.children(".fa-angle-up").first().removeClass("fa-angle-up").addClass("fa-angle-down");
                        btn.parent("li").removeClass("active");
                    } else {
                        //Slide down to open menu
                        menu.slideDown();
                        isActive = true;
                        btn.children(".fa-angle-down").first().removeClass("fa-angle-down").addClass("fa-angle-up");
                        btn.parent("li").addClass("active");
                    }

                }
            });

            /* Add margins to submenu elements to give it a tree look */


        });

    };


}(jQuery));